package com.card.game;

import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

import com.card.common.MessageComm;
import com.card.message.JsonMessageCenter;
import com.card.user.User;
import com.card.user.UserUtils;
import com.xiaoao.net.json.message.JsonMessage;

public class ZjhPlayer extends BasePlayer{
	public transient ZjhInfo zjhInfo;
	private transient Hand hand = new Hand();
	private transient int type = -1;
	
	public ZjhPlayer(User user) {
		super(user);
		this.zjhInfo = new ZjhInfo(user.getPrimaryKey());
	}
	
	public void initCards(Vector<Card> cards) {
		Collections.sort(cards,new Comparator<Card>() {
			@Override
			public int compare(Card o1, Card o2) {
				return o1.getIndex()-o2.getIndex();
			}
		});
		this.type = calType(cards);
		this.hand.cards = cards;
	}
	
	private int calType(Vector<Card> cards) {
		if(ZjhUtil.isBaozi(cards)) {
			return JHType.Baozi;
		}else if(ZjhUtil.isTongHua(cards) && ZjhUtil.isLianZi(cards)) {
			return JHType.THS;
		}else if(ZjhUtil.isTongHua(cards) && ZjhUtil.isDui(cards)) {
			return JHType.THD;
		}else if(ZjhUtil.isTongHua(cards)) {
			return JHType.TH;
		}else if(ZjhUtil.isLianZi(cards)) {
			return JHType.LianZi;
		}else if(ZjhUtil.isDui(cards)) {
			return JHType.Dui;
		}
		return JHType.Dan;
	}
	
	public Vector<Card> getShowCards() {
		if(zjhInfo.isKanPai) {
			return hand.cards;
		}
		return CardUtils.AnPai;
	}
	
	public Vector<Card> getTrueCards() {
		return hand.cards;
	}
	
	public Vector<Card> getOtherCards(ZjhPlayer player) {
		if(this.zjhInfo.isContains(player.getTableIndex())) {
			return player.getTrueCards();
		}else{
			return CardUtils.AnPai;
		}
	}
	
	public Vector<Card> getEndCard() {
		if(this.zjhInfo.isQiPai) {
			return CardUtils.AnPai;
		}else{
			return getTrueCards();
		}
	}
	public Vector<Card> getEndMyCard() {
		if(this.zjhInfo.isQiPai && !this.zjhInfo.isKanPai) {
			return CardUtils.AnPai;
		}else{
			return getTrueCards();
		}
	}
	
	public int getType() {
		if(!zjhInfo.isKanPai) {
			return -1;
		}
		return type;
	}
	
	public void sendHandMessage() {
		JsonMessage message = new JsonMessage(MessageComm.S2C_HandUpdate2Out);
		message.addProperty("hand", getShowCards());
		message.addProperty("type", getType());
		JsonMessageCenter.getInstance().sendMessage(message, this);
	}
	
	public boolean is235Card() {
		return type == JHType.Dan && ZjhUtil.is235Card(hand.cards);
	}
	
	public Card getDuiCard() {
		return hand.cards.get(1);
	}
	
	public int compare(ZjhPlayer temp) {
		if(type == JHType.Baozi && temp.is235Card()) {
			return -1;
		}
		if(temp.type == JHType.Baozi && is235Card()) {
			return 1;
		}
		int v = type - temp.type;
		if(v == 0) {
			if(type == JHType.Dui || type == JHType.THD) {
				int c1 = getDuiCard().getIndex();
				int c2 = temp.getDuiCard().getIndex();
				if(c1 != c2) {
					return c1 - c2;
				}
			}
			Vector<Card> c1 = hand.cards;
			Vector<Card> c2 = temp.hand.cards;
			for (int i = 2; i >= 0; i--) {
				if(c1.get(i).getIndex() > c2.get(i).getIndex()) {
					return 1;
				}else if(c1.get(i).getIndex() < c2.get(i).getIndex()) {
					return -1;
				}
			}
			return 0;
		}
		return v;
	}
	
	public void clearData() {
		this.hand.clear();
		this.type = -1;
		this.zjhInfo.clear();
	}
	
	public static void main(String[] args) {
		Vector<Card> c1 = CardUtils.testCards("1-7;2-7;1-13");
		Vector<Card> c2 = CardUtils.testCards("1-5;1-8;2-8");
		ZjhPlayer p1 = new ZjhPlayer(UserUtils.getUserByImei("1"));
		ZjhPlayer p2 = new ZjhPlayer(UserUtils.getUserByImei("2"));
		p1.initCards(c1);
		p2.initCards(c2);
		
		System.err.println(p1.compare(p2));
	}
}
